package cn.south.campus.business.controller;

import cn.south.campus.business.model.req.GoodsOrderCreateReq;
import cn.south.campus.business.model.req.GoodsOrderQueryReq;
import cn.south.campus.business.model.vo.GoodsOrderVo;
import cn.south.campus.business.service.GoodsOrderService;
import cn.south.campus.common.model.bo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 *
 * </p>
 *
 * @author chow
 * @since 2024/11/29 9:09
 */
@RestController
@RequestMapping("/campus/goods/order")
@AllArgsConstructor
public class GoodsOrderController {

    private final GoodsOrderService goodsOrderService;

    @PostMapping("/create")
    public Result<String> create(@RequestBody @Valid GoodsOrderCreateReq goodsOrderReq) {
        String orderNo = goodsOrderService.createOrder(goodsOrderReq);
        return Result.success(orderNo);
    }

    @PutMapping("/cancel/{orderNo}")
    public Result<String> cancel(@PathVariable @NotNull(message = "订单编号不能为空") String orderNo) {
        goodsOrderService.cancelOrder(orderNo);
        return Result.success("取消成功");
    }

    @PutMapping("/pay")
    public Result<String> pay(@NotNull(message = "订单编号不能为空") String orderNo, @NotNull(message = "支付方式不能为空") String payMethod) {
        goodsOrderService.payOrder(orderNo, payMethod);
        return Result.success("支付成功");
    }

    @GetMapping("buyer/page")
    public Result<IPage<GoodsOrderVo>> page(GoodsOrderQueryReq req) {
        return Result.success(goodsOrderService.pageBuyer(req));
    }

    @GetMapping("seller/page")
    public Result<IPage<GoodsOrderVo>> pageSeller(GoodsOrderQueryReq req) {
        return Result.success(goodsOrderService.pageSeller(req));
    }

    @GetMapping("buyer/detail/{orderNo}")
    public Result<GoodsOrderVo> detail(@PathVariable @NotNull(message = "订单编号不能为空") String orderNo) {
        return Result.success(goodsOrderService.getBuyerOrderDetail(orderNo));
    }

    @GetMapping("seller/detail/{orderNo}")
    public Result<GoodsOrderVo> detailSeller(@PathVariable @NotNull(message = "订单编号不能为空") String orderNo) {
        return Result.success(goodsOrderService.getSellerOrderDetail(orderNo));
    }

    @PutMapping("confirm/{orderNo}")
    public Result<String> confirm(@PathVariable @NotNull(message = "订单编号不能为空") String orderNo) {
        goodsOrderService.confirmOrder(orderNo);
        return Result.success("确认收货成功");
    }
    @PutMapping("confirmDeliver/{orderNo}")
    public Result<String> confirmDeliver(@PathVariable @NotNull(message = "订单编号不能为空") String orderNo) {
        goodsOrderService.confirmDeliver(orderNo);
        return Result.success("交货成功");
    }

    @PutMapping("applyRefund/{orderNo}")
    public Result<String> comment(@PathVariable @NotNull(message = "订单编号不能为空") String orderNo) {
        goodsOrderService.applyRefund(orderNo);
        return Result.success("申请退款成功");
    }
    @PutMapping("cancelRefund/{orderNo}")
    public Result<String> cancelRefund(@PathVariable @NotNull(message = "订单编号不能为空") String orderNo) {
        goodsOrderService.cancelRefund(orderNo);
        return Result.success("申请退款成功");
    }

    @PutMapping("refund/{orderNo}")
    public Result<String> refund(@PathVariable @NotNull(message = "订单编号不能为空") String orderNo) {
        goodsOrderService.refundOrder(orderNo);
        return Result.success("退款成功");
    }

    @DeleteMapping("delete/{orderNo}")
    public Result<String> delete(@PathVariable @NotNull(message = "订单编号不能为空") String orderNo) {
        goodsOrderService.deleteOrder(orderNo);
        return Result.success("删除成功");
    }
    @DeleteMapping("deleteMe/{orderNo}")
    public Result<String> deleteMe(@PathVariable @NotNull(message = "订单编号不能为空") String orderNo) {
        goodsOrderService.deleteMeOrder(orderNo);
        return Result.success("删除成功");
    }
}
